package yannan.voiplock4ble.base;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;

/**
 * 说明：Fragment公共基类
 * 作者：mainTel
 * 时间：2016/8/19 15:32
 * 备注：
 */
public abstract class BaseFragment extends Fragment {

    protected Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = null;
        Class<? extends BaseFragment> attr = getClass();
        Content annotation = attr.getAnnotation(Content.class);
        if (annotation != null) {
            int value = annotation.value();
            view = setContentView(value);
            ButterKnife.bind(this, view);
        } else {
            TextView tv = new TextView(mContext);
            tv.setText("请在类前加上@content注解,或者重写OncreateView方法");
            tv.setBackgroundColor(Color.RED);
            view = tv;
        }
        EventBus.getDefault().register(this);
        return view;


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initView();
    }

    public abstract void initData();

    public abstract void initView();

    public View setContentView(int layoutId) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layoutId, null);
        return view;
    }

    public View findViewById(int id) {
        return getView().findViewById(id);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void baseEvent(String device) {
    }
}
